#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
#    文本邮件发送脚本


from email.mime.text import MIMEText
import logging
import smtplib


# #发送report邮箱信息
MAIL_HOST = "smtp.example.com"
MAIL_USER = "alter@example.com"
MAIL_PWD = "password"
# 
# #接收 report 邮箱地址
# MAIL_TO = ['user1@example.com','user2@example.com','user3@example.com']


#日志设置
logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',  
                    datefmt='%a, %d %b %Y %H:%M:%S',  
                    filename='/tmp/email.log',  
                    filemode='a')


# body = (data,'html',utf-8)
def send_mail(to_list, subject, body):
    if not body or not subject:
        exit
    msg = MIMEText(body,'html','utf-8')
    msg['Subject'] = subject
    msg['From'] = MAIL_USER
    msg['To'] = ','.join(to_list)
    
    try:
        smtp = smtplib.SMTP()
        smtp.connect(MAIL_HOST)
        smtp.login(MAIL_USER, MAIL_PWD)
        smtp.sendmail(MAIL_USER, to_list, msg.as_string())
        smtp.quit()
        logging.info((subject,to_list))
        print 'OK'
        return True
    except Exception, e:
        logging.info(e)
        return False